<?php

namespace app\api\controller;

use app\common\model\SmsSendRecordModel;
use app\common\model\SmsNameListModel;
use think\App;


/**
 * 短信
 */
class Sms extends Base
{
    // 初始化构造方法
    protected $smsSendRecordModel;
    protected $smsNameListModel;

    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->smsSendRecordModel = new SmsSendRecordModel();
        $this->smsNameListModel = new SmsNameListModel();
    }

    //发送短信并写记录
    public function sendBamiSms()
    {
        if (empty($this->parm['signature'])) {
            returnJson(1, "缺少参数,短信签名");
        }
        if (empty($this->parm['account'])) {
            returnJson(1, "缺少参数,短信平台uid");
        }
        if (empty($this->parm['passwd'])) {
            returnJson(1, "缺少参数,短信平台账号");
        }
        if (empty($this->parm['content'])) {
            returnJson(1, "缺少参数,短信内容");
        }
        if (empty($this->parm['phone'])) {
            returnJson(1, "缺少参数,手机号");
        }
        $signature = $this->parm['signature'];
        $content = $this->parm['content'];
        $account = $this->parm['account'];
        $passwd = $this->parm['passwd'];
        $phone = $this->parm['phone'];
        $app_sn = $this->parm['app_sn'] ?? 'cloud2019';
        //发送限制判断：1.黑名单不发送 2.白名单不限制 3.每个手机号每天只能接收10次
        $black_count = $this->smsNameListModel->getCount(['type' => 2, 'phone' => $phone]);
        if (!empty($black_count)) {
            returnJson(1, "发送失败!" . $phone . '处于黑名单');
        }
        $white_count = $this->smsNameListModel->getCount(['type' => 1, 'phone' => $phone]);
        if ($white_count == 0) {
            $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
            $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
            $where[] = ['phone', '=', $phone];
            $where[] = ['app_sn', '=', $app_sn];
            $where[] = ['create_time', 'between', [$beginToday, $endToday]];
            $send_count = $this->smsSendRecordModel->getCount($where);
            $max = 10;//发送最大次数
            if ($send_count > $max) {
                returnJson(1, '每个手机号每天仅能接收' . $max . '条信息');
            }
        }
        $s_content = urlencode($signature) . "。【" . $content . "】";
        $gateway = "http://sms.bamikeji.com:8890/mtPort/mt/normal/send?uid=" . $account . "&passwd=" . md5($passwd) . "&phonelist=" . $phone . "&content=" . $s_content;
        $result = file_get_contents($gateway);
        $result = json_decode($result, true);
        //需要判断接口状态
        if (isset($result['code']) && $result['code'] == 0) {
            $record['app_sn'] = $app_sn;
            $record['platform_account'] = $account;
            $record['platform_source'] = 'BAMI';
            $record['signature'] = $signature;
            $record['phone'] = $phone;
            $record['content'] = $content;
            $record['create_time'] = time();
            $this->smsSendRecordModel->add($record);
            returnJson(0, "发送成功");
        } else {
            returnJson(1, "发送失败" . $result['msg'] ?? '');
        }
    }

    /**
     * Notes:短信余额查询
     * @param string $uid
     * @param string $passwd
     * @return mixed
     */
    public function getSmsMoney($uid, $passwd)
    {
        $url = "http://sms.bamikeji.com:8890/mtPort/account/info?uid=" . $uid . "&passwd=" . md5($passwd);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        return json_decode($output);
    }
}